
package labb3;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class viewControl extends JFrame implements ActionListener {
    
    JPanel mainPanel, northPanel;
    gameBoard playerBoard, compBoard;
    String retHand, hand, winner;
    TextClient model;
    private int count = 0;
    private String[] ord = {"ETT", "TVÃ…"};
    
    public viewControl(String compName, TextClient txtcl, String playerName){
        setSize(new Dimension(400,650));
        model = txtcl;
        mainPanel = new JPanel(new FlowLayout());
        northPanel = new JPanel(new FlowLayout());
        Container contentPane = this.getContentPane();
        contentPane.setLayout(new BorderLayout());
        
        contentPane.add(mainPanel, BorderLayout.CENTER);
        contentPane.add(northPanel, BorderLayout.NORTH);
        
        northPanel.add(new JLabel(model.getGreet(hand)));
        mainPanel.add(playerBoard = new gameBoard(playerName, this));
        mainPanel.add(compBoard = new gameBoard(compName));
       
        
        //Add exitbutton
        northPanel.add(new JButton(new AbstractAction("Avsluta") {
        public void actionPerformed(ActionEvent e) {
            //model.play(null);
            System.exit(0);
        }
        }));
        
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);   
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        hand = e.getActionCommand();
        playerBoard.resetColor();
        compBoard.resetColor();
        click(hand);
        
    }
    
    public void click(String hnd){
        if(count == 2){
            retHand = model.compplay(hnd);
            playerBoard.markPlayed(hnd);
            System.out.println(retHand);
            compBoard.markPlayed(retHand);
            count = 0;
            winner = model.winner();
                if (winner.equals("cmp")){
                    playerBoard.setLower("lose");
                    compBoard.setLower("win");
                    compBoard.wins();
                } else if (winner.equals("player")) {
                    playerBoard.setLower("win");
                    compBoard.setLower("lose");
                    playerBoard.wins();
                } else {
                    playerBoard.setLower("draw");
                    compBoard.setLower("draw");
                }
        } else if (count==0 || count==1){
            playerBoard.setLower(ord[count]);
            System.out.println(Integer.toString(count));
            count++;
    }
    }
    
    
    
}

